package vn.vfriends.soffice.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Size;

/**
 *
 * @author chaungoctuan@gmail.com
 */
@Entity
@Table(name="SYSGROUP")
@NamedQueries({
    @NamedQuery(name = "Group.findByCode", query = "SELECT g FROM Group g WHERE g.code = :code"),
    @NamedQuery(name = "Group.findByName", query = "SELECT g FROM Group g WHERE g.name = :name")})
public class Group implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Integer id;
    
    @Size(max=50)
    @Column(length=50, nullable=false, unique=true)
    private String name;
    
    @Size(max=10)
    @Column(length=10, nullable=false, unique=true)
    private String code;
    
    @Size(max = 200)
    @Column(length = 200)
    private String description;
    
    @ManyToMany(cascade= CascadeType.MERGE)
    @JoinTable(name = "GROUP_MENU", joinColumns = @JoinColumn(name = "GROUP_ID"), 
            inverseJoinColumns = @JoinColumn(name = "MENU_ID"))
    private List<Menu> menus = new ArrayList<Menu>();
    
    @ManyToMany(cascade= CascadeType.MERGE)
    @JoinTable(name = "GROUP_PERMISSION", joinColumns = @JoinColumn(name = "GROUP_ID"), 
            inverseJoinColumns = @JoinColumn(name = "PERMISSION_ID"))
    private List<Permission> permissions = new ArrayList<Permission>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(List<Menu> menus) {
        this.menus = menus;
    }

    public List<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Group other = (Group) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
}
